<?php

namespace App\V3\V3_2_7\Models;

use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;

/**
 * 退货商品
 * Class SalesReturnGoodsDetailModel
 * @package App\Models
 */
class SalesReturnGoodsDetailModel extends Model
{


    protected $table = 'sales_return_goods_detail';

    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储
    //toArray 时间转换问题
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function content()
    {
        return self::hasMany(MaterielProductContentModel::class, 'product_id', 'materiel_type_id');
    }

    public function GoodsSerialInfo()
    {

        return SELF::hasMany(SalesOutSerialNumberModel::class, 'return_goods_detail_id', 'id');
    }

    public function GoodsBatchInfo()
    {
        return SELF::hasMany(SalesReturnGoodsBatchModel::class, 'return_goods_detail_id', 'id');

    }
    static public function getGoodsDetailInfo($salesId, $condition)
    {
        return self::select(
            'return_id'
        )
            ->from('sales_return_goods_detail as a')
            ->leftJoin('materiel_type as b', 'a.materiel_type_id', '=', 'b.id')
            ->where($condition)
            ->whereIn('return_id', $salesId)
            ->distinct()
            ->get()->toArray();
    }

    /**
     * 退货货商品列表
     * @param unknown $sellId
     * @return unknown
     */
    static public function detail($id)
    {
        return self::select(
            'sales_return_goods_detail.id',
            'sales_return_goods_detail.id as return_goods_id',
            'a.id as type_id',
            'a.materiel_name',
            'a.type_name',
            'a.materiel_no',
            'a.materiel_img',
            'price',
            'sales_return_goods_detail.num',
            'sales_return_goods_detail.in_num',
            'b.storage_name',
            'b.id as storage_id',
            'return_id',
            'cost_price',
            'sales_return_goods_detail.shelf_id',
            'sales_return_goods_detail.location_id',
            's.shelf_name',
            'l.location_name',
            'a.is_use_serial',
            'a.materiel_class_id',
            'a.materiel_unit',
            'a.is_product_materiel',
            'a.is_use_serial',
            'a.texture',
            'a.executive_standard',
            'sales_return_goods_detail.remark',
            'sales_return_goods_detail.batch_info'
            )
            ->where('return_id', $id)
            ->where('sales_return_goods_detail.delete_flag',0)
            ->where('a.delete_flag',0)
            ->where('b.delete_flag',0)
            ->where('s.status',1)
            ->where('l.status',1)
            ->leftJoin('materiel_type as a', 'a.id', '=', 'sales_return_goods_detail.materiel_type_id')
            ->leftJoin('storage as b', 'b.id', '=', 'sales_return_goods_detail.warehouse_id')
            ->leftJoin('storage_shelf as s','sales_return_goods_detail.shelf_id','=','s.id')
            ->leftJoin('storage_shelf_location as l','sales_return_goods_detail.location_id','=','l.id')
            ->get()->toArray();
    }

    static public function getBarterReturnGoodsDetailInfo($userId, $data)
    {
        $query = SELF::select('return_id as id')
        ->from('sales_return_goods_detail as a')
        ->leftJoin('materiel_type as b','b.id','=','a.materiel_type_id')
        ->where([
            'a.main_id' => $data['main_id'],
            'a.delete_flag' => 0
        ]);
        if(!empty($data['sales_return_idArr'])){
            $query->whereIn('return_id',$data['sales_return_idArr']);
        }
        return $query->where(function($query) use ($data) {
                    if($data['search_type'] == 1){
                        $query->where('b.materiel_name', 'like', '%'.$data['keyword'].'%');
                    }else{
                        $query->where('b.materiel_no', 'like', '%'.$data['keyword'].'%');
                    }
                })->get()->toArray();
    }
}
